package com.hp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.entity.Drug;
import com.hp.entity.MedicalRecord;
import com.hp.service.DrugService;
import com.hp.service.MedicalRecordService;
import com.hp.util.ResultJson;
import com.hp.util.Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/medicalRecord")
public class MedicalRecordController {
    @Resource
    private MedicalRecordService medicalRecordService;
    @RequestMapping("/findAll")
    private void findAll(MedicalRecord medicalRecord, @RequestParam(value = "page",defaultValue = "1")Integer page,
                         @RequestParam(value = "limit",defaultValue = "10")Integer limit, HttpServletResponse response){
        System.out.println(medicalRecord);
        PageHelper.startPage(page,limit);
        List<MedicalRecord> list = medicalRecordService.findAll(medicalRecord);
        System.out.println(list);
        if (list.size() != 0) {
            Utils.returnJson(ResultJson.success(new PageInfo<>(list)), response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/findById")
    private void findById(MedicalRecord medicalRecord, HttpServletResponse response){
        System.out.println(medicalRecord);
        MedicalRecord medicalRecord1 = medicalRecordService.findById(medicalRecord);
        System.out.println(medicalRecord1);
        if (medicalRecord1 != null) {
            Utils.returnJson(ResultJson.success(medicalRecord1), response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/delete")
    private void delete(String[] id, HttpServletResponse response){
        int i  = medicalRecordService.delete(id);
        if (i > 0) {
            Utils.returnJson(i, response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/add")
    private void add(MedicalRecord medicalRecord, HttpServletResponse response){
        int i  = medicalRecordService.add(medicalRecord);
        if (i > 0) {
            Utils.returnJson(i, response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/update")
    private void update(MedicalRecord medicalRecord, HttpServletResponse response){
        int i  = medicalRecordService.update(medicalRecord);
        if (i > 0) {
            Utils.returnJson(i, response);
        } else {
            Utils.returnJson(ResultJson.error(), response);
        }
    }
}
